package org.linlinjava.litemall.admin.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.linlinjava.litemall.admin.job.OrderJob;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.constant.CommissionConstant;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallCommissionApplyService;
import org.linlinjava.litemall.db.service.LitemallCommissionLogService;
import org.linlinjava.litemall.db.service.LitemallCommissionService;
import org.linlinjava.litemall.db.service.LitemallOrderCommissionService;
import org.linlinjava.litemall.db.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 佣金管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/commission")
@Api(description = "会员佣金管理")
public class AdminCommissionController {
    @Autowired
    private LitemallOrderCommissionService orderCommissionService;
    @Autowired
    private LitemallCommissionLogService commissionLogService;
    @Autowired
    private LitemallCommissionApplyService commissionApplyService;
    @Autowired
    private LitemallCommissionService commissionService;
    @Autowired
    private OrderJob orderJob;

    @ResponseBody
    @PostMapping("queryLog")
    @ApiOperation(value = "佣金变动日志查询", httpMethod = "POST")
    public Object queryLog(@RequestBody PageVO<LitemallCommissionLog, LitemallCommissionLog> pageVO) {
        List<LitemallCommissionLog> list = commissionLogService.queryByExample(pageVO);
        return ResponseUtil.okList(list);
    }

    @ResponseBody
    @PostMapping("queryOrder")
    @ApiOperation(value = "佣金发放日志查询", httpMethod = "POST")
    public Object queryOrder(@RequestBody PageVO<LitemallOrderCommission, LitemallOrderCommission> pageVO) {
        List<LitemallOrderCommission> list = orderCommissionService.queryByExample(pageVO);
        return ResponseUtil.okList(list);
    }


    @ResponseBody
    @PostMapping("queryApply")
    @ApiOperation(value = "佣金提现记录查询", httpMethod = "POST")
    public Object commissionApply(@RequestBody PageVO<LitemallCommissionApply, LitemallCommissionApply> pageVO) {
        try {
            List<LitemallCommissionApply> list = commissionApplyService.queryByExample(pageVO);
            if (list == null) {
                return ResponseUtil.serious();
            }
            return ResponseUtil.okList(list);
        } catch (Exception e) {
            log.error("提现申请记录查询异常：" + e);
            return ResponseUtil.serious();
        }
    }

    @ResponseBody
    @PostMapping("updateApply")
    @ApiOperation(value = "佣金提现申请更新", httpMethod = "POST")
    public Object commissionApply(@RequestBody LitemallCommissionApply commissionApply) {
        try {
            if (StringUtils.isEmpty(commissionApply.getId())) {
                return ResponseUtil.fail(0000, "请选择提现申请记录");
            }
            commissionApply.setUpdatedTime(LocalDateTime.now());
            int result = commissionApplyService.update(commissionApply);
            if (result < 0) {
                return ResponseUtil.serious();
            }
            if (CommissionConstant.APPLY_PAY.equals(commissionApply.getStatus())) {
                LitemallCommissionApplyExample example = new LitemallCommissionApplyExample();
                example.createCriteria().andIdEqualTo(commissionApply.getId());
                LitemallCommissionApply apply = commissionApplyService.queryOneByExample(example);
                //提现转账确认，扣减佣金
                LitemallCommission commission = new LitemallCommission();
                commission.setCurrentCommission(commission.getCurrentCommission().subtract(apply.getCommission()));
                commission.setUserId(apply.getUserId());
                commission.setApplyCommission(commission.getApplyCommission().add(apply.getCommission()));
                //用户佣金刷新
                commissionService.updateCommission(commission, null, commissionApply);
            }
            return ResponseUtil.ok();
        } catch (Exception e) {
            log.error("佣金提现申请更新异常：" + e);
            return ResponseUtil.serious();
        }
    }

    @GetMapping("task")
    @ApiOperation(value = "task", httpMethod = "GET")
    public Object commissionTask() {
        try {
            orderJob.orderCommission();
            return ResponseUtil.ok();
        } catch (Exception e) {
            log.error("订单佣金发放任务执行异常：", e);
            return ResponseUtil.serious();
        }
    }
}
